from tortoise import BaseDBAsyncClient


async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        CREATE TABLE IF NOT EXISTS `aerich` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `version` VARCHAR(255) NOT NULL,
    `app` VARCHAR(100) NOT NULL,
    `content` JSON NOT NULL
) CHARACTER SET utf8mb4;
CREATE TABLE IF NOT EXISTS `department` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
    `dept_id` VARCHAR(225) NOT NULL COMMENT '部门ID',
    `dept_name` VARCHAR(100) NOT NULL COMMENT '部门名称',
    `description` LONGTEXT COMMENT '部门描述',
    `parent_id` VARCHAR(225) COMMENT '父级部门ID',
    `ancestors` VARCHAR(255) COMMENT '部门路径',
    `level` INT NOT NULL COMMENT '部门层级' DEFAULT 1,
    `order_num` INT NOT NULL COMMENT '部门排序' DEFAULT 0,
    `created_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4 COMMENT='部门表';
CREATE TABLE IF NOT EXISTS `permission` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增主键',
    `name` VARCHAR(100) NOT NULL UNIQUE COMMENT '权限名称',
    `code` VARCHAR(100) NOT NULL UNIQUE COMMENT '权限代码',
    `description` LONGTEXT COMMENT '权限描述',
    `created_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)
) CHARACTER SET utf8mb4 COMMENT='权限表';
CREATE TABLE IF NOT EXISTS `role` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增主键',
    `name` VARCHAR(32) NOT NULL UNIQUE COMMENT '角色名称',
    `code` VARCHAR(32) NOT NULL UNIQUE COMMENT '角色代码',
    `description` LONGTEXT COMMENT '角色描述',
    `created_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `department_id` INT COMMENT '所属部门',
    CONSTRAINT `fk_role_departme_0e17af14` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='角色表';
CREATE TABLE IF NOT EXISTS `user` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增主键',
    `username` VARCHAR(32) NOT NULL UNIQUE COMMENT '用户名',
    `hashed_password` VARCHAR(128) NOT NULL COMMENT '哈希密码',
    `name` VARCHAR(32) COMMENT '姓名',
    `gender` VARCHAR(32) COMMENT '性别',
    `birthday` DATE COMMENT '生日',
    `address` VARCHAR(128) COMMENT '地址',
    `email` VARCHAR(128) COMMENT '邮箱',
    `phone` VARCHAR(128) COMMENT '手机号',
    `bio` LONGTEXT COMMENT '个人简介',
    `avatar` VARCHAR(128) COMMENT '头像',
    `status` VARCHAR(32) NOT NULL COMMENT '状态' DEFAULT '启用',
    `is_superuser` BOOL NOT NULL COMMENT '是否超级用户' DEFAULT 0,
    `is_active` BOOL NOT NULL COMMENT '是否激活' DEFAULT 1,
    `created_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `department_id` INT COMMENT '所属部门',
    CONSTRAINT `fk_user_departme_1bf28910` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`) ON DELETE CASCADE,
    KEY `idx_user_usernam_9987ab` (`username`)
) CHARACTER SET utf8mb4 COMMENT='用户表';
CREATE TABLE IF NOT EXISTS `login_history` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增主键',
    `login_time` DATETIME(6) NOT NULL COMMENT '登录时间' DEFAULT CURRENT_TIMESTAMP(6),
    `ip_address` VARCHAR(64) NOT NULL COMMENT 'IP地址',
    `user_agent` VARCHAR(256) NOT NULL COMMENT '浏览器标识',
    `browser` VARCHAR(32) COMMENT '浏览器类型',
    `device` VARCHAR(128) COMMENT '设备信息',
    `os` VARCHAR(64) COMMENT '操作系统',
    `location` VARCHAR(128) COMMENT '登录地点',
    `status` VARCHAR(32) NOT NULL COMMENT '登录状态' DEFAULT '成功',
    `user_id` INT NOT NULL COMMENT '关联用户',
    CONSTRAINT `fk_login_hi_user_b0f44f01` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='用户登录历史表';
CREATE TABLE IF NOT EXISTS `request_log` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增主键',
    `request_time` DATETIME(6) NOT NULL COMMENT '请求时间' DEFAULT CURRENT_TIMESTAMP(6),
    `ip_address` VARCHAR(64) NOT NULL COMMENT 'IP地址',
    `user_agent` VARCHAR(256) NOT NULL COMMENT '浏览器标识',
    `browser` VARCHAR(32) COMMENT '浏览器类型',
    `path` VARCHAR(256) NOT NULL COMMENT '请求路径',
    `method` VARCHAR(16) NOT NULL COMMENT '请求方法',
    `status_code` INT NOT NULL COMMENT '响应状态码',
    `params` JSON COMMENT '请求参数'
) CHARACTER SET utf8mb4 COMMENT='通用请求日志表';
CREATE TABLE IF NOT EXISTS `agent` (
    `id` INT NOT NULL PRIMARY KEY AUTO_INCREMENT COMMENT '自增主键',
    `name` VARCHAR(64) NOT NULL COMMENT '智能体名称',
    `description` LONGTEXT COMMENT '智能体描述',
    `avatar` VARCHAR(255) COMMENT '头像',
    `category` VARCHAR(64) COMMENT '智能体分类',
    `agent_type` VARCHAR(32) NOT NULL COMMENT '智能体类型包括类型（内嵌、外链）',
    `status` VARCHAR(32) NOT NULL COMMENT '智能体状态，如：active, inactive, deleted等',
    `config` JSON COMMENT '智能体配置: 接口url，模型id，apikey，max_token等',
    `version` VARCHAR(16) NOT NULL COMMENT '版本号' DEFAULT '1.0.0',
    `created_at` DATETIME(6) NOT NULL COMMENT '创建时间' DEFAULT CURRENT_TIMESTAMP(6),
    `updated_at` DATETIME(6) NOT NULL COMMENT '更新时间' DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
    `created_by_id` INT COMMENT '创建者',
    `department_id` INT COMMENT '所属部门',
    CONSTRAINT `fk_agent_user_5906f213` FOREIGN KEY (`created_by_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
    CONSTRAINT `fk_agent_departme_01494a73` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`) ON DELETE CASCADE
) CHARACTER SET utf8mb4 COMMENT='智能体表';
CREATE TABLE IF NOT EXISTS `department_permission` (
    `department_id` INT NOT NULL,
    `permission_id` INT NOT NULL,
    FOREIGN KEY (`department_id`) REFERENCES `department` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`permission_id`) REFERENCES `permission` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_department__departm_f9cbc3` (`department_id`, `permission_id`)
) CHARACTER SET utf8mb4 COMMENT='部门拥有的权限';
CREATE TABLE IF NOT EXISTS `role_permission` (
    `role_id` INT NOT NULL,
    `permission_id` INT NOT NULL,
    FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`permission_id`) REFERENCES `permission` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_role_permis_role_id_7454bb` (`role_id`, `permission_id`)
) CHARACTER SET utf8mb4 COMMENT='角色拥有的权限';
CREATE TABLE IF NOT EXISTS `user_role` (
    `user_id` INT NOT NULL,
    `role_id` INT NOT NULL,
    FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON DELETE CASCADE,
    FOREIGN KEY (`role_id`) REFERENCES `role` (`id`) ON DELETE CASCADE,
    UNIQUE KEY `uidx_user_role_user_id_d0bad3` (`user_id`, `role_id`)
) CHARACTER SET utf8mb4 COMMENT='用户拥有的角色';"""


async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        """
